<?php
/*
 * @Author: 张逸飞 zhixiao476@gmail.com
 * @Date: 2023-07-24 17:13:59
 * @LastEditors: 张逸飞 zhixiao476@gmail.com
 * @LastEditTime: 2023-07-24 17:17:46
 */
namespace App\Http\Controllers;

use Illuminate\Routing\Controller;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Support\Facades\Auth;

class BaseController extends Controller
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected array $view_data = [];

    protected int $user_id = 0;

    public function render($view = null, $data = [])
    {
        return view($view, $data);
    }

    /**
     * 接口结果
     *
     * @param        $data
     * @param string $message
     * @param int    $code
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function apiSuccess($data, $message = '', $code = 0)
    {

        return response()->json([
            'code'      => $code,
            'msg'       => $message,
            'body'      => $data,
        ], 200, [], JSON_UNESCAPED_UNICODE);
    }

    public function apiError($message = '', $code = 400, $data = [])
    {
        return response()->json([
            'code'      => $code,
            'msg'       => $message,
            'body'      => $data,
        ], 200,  [], JSON_UNESCAPED_UNICODE);
    }

    public function errorPage($message = '') {
        return view('error', ['info' => $message]);
    }

    public function getBasicClassName($str) {
        return array_last(explode("\\", $str));
    }

}
